package com.dailyblue.java.project.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.java.project.community.mapper.CommunityMapper;
import com.dailyblue.java.project.community.service.CommunityService;
import com.dailyblue.project.estate.commons.bean.Community;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Slf4j
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {

    @Cacheable(cacheNames = "findAllCommunity", key = "0")
    public JsonResult find() {
        return ResultTool.success(list());
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    @CachePut(cacheNames = "findAllCommunity", key = "0")
    public JsonResult saveCommunity(Community community) {
        save(community);
        return find();
    }

    @Cacheable(cacheNames = "searchCommunityByCreateTime", key = "#begin+'.'+#end+'.'+#code")
    public JsonResult searchCreateTime(String begin, String end, String code) {
        if (begin == null || begin.trim().equals("")) {
            begin = null;
        }
        if (end == null || end.trim().equals("")) {
            end = null;
        }
        if (code == null || code.trim().equals("")) {
            code = null;
        } else {
            code = "%" + code + "%";
        }
        return ResultTool.success(getBaseMapper().findByCreateTime(begin, end, code));
    }
}
